<?php require('connections/con_gl.php'); ?>
<?php
cekAkses($_SESSION[akses],"PD-6");
$currentPage = $_SERVER["PHP_SELF"];

$maxRows_data = 8;
$pageNum_data = 0;
if (isset($_GET['pageNum_data'])) {
  $pageNum_data = $_GET['pageNum_data'];
}
$startRow_data = $pageNum_data * $maxRows_data;
$cr_txt = str_replace("+"," ",$_REQUEST[cr_txt]);
$query_jurnal = "SELECT gl_rtrans.no_ref as NoReferensi, gl_rtrans.tgl as Tanggal, gl_barang.barang as Barang, gl_trans.jumlah as Jumlah, gl_satuan.satuan as Satuan, gl_trans.diskon as Diskon, gl_trans.hargasatuan as Harga, gl_trans.total as Total FROM gl_rtrans, gl_trans, gl_barang, gl_satuan WHERE (gl_rtrans.jenis=6 OR gl_rtrans.jenis=8) AND gl_trans.no_ref=gl_rtrans.no_ref AND gl_barang.id AND gl_trans.barang AND gl_satuan.id AND gl_trans.satuan";
if(!strstr($_SESSION[akses],'A-1-4')) {
	$query_jurnal .= " AND gl_rtrans.gudang='$_SESSION[lokasi]'";
}
mysql_select_db($database_con_gl, $con_gl);
$query_data = "SELECT gl_trans.id, gl_rtrans.jenis, gl_rtrans.no_ref, gl_rtrans.tgl, gl_trans.jumlah, gl_trans.diskon, gl_trans.hargasatuan, gl_trans.total, gl_barang.barang, gl_satuan.satuan FROM gl_rtrans, gl_trans, gl_barang, gl_satuan WHERE (gl_rtrans.jenis=6 OR gl_rtrans.jenis=7) AND gl_trans.no_ref=gl_rtrans.no_ref AND gl_barang.id = gl_trans.barang AND gl_satuan.id = gl_trans.satuan";
if(!strstr($_SESSION[akses],'A-1-4')) {
	$query_data .= " AND gl_rtrans.gudang='$_SESSION[lokasi]'";
}
if($_GET[cr_txt]<>'' AND $_GET[cr_txt]<>'+') {
	$query_data .=" AND (gl_rtrans.no_ref='$cr_txt' OR gl_rtrans.tgl='$cr_txt' OR gl_barang.barang='$cr_txt')";
	$query_jurnal .=" AND (gl_rtrans.no_ref='$cr_txt' OR gl_rtrans.tgl='$cr_txt' OR gl_barang.barang='$cr_txt')";
}
if($_GET[mulai]<>'' AND $_GET[sampai]<>'') {
	$query_data .=" AND (gl_rtrans.tgl >='$_GET[mulai]' AND gl_rtrans.tgl <= '$_GET[sampai]')";
	$query_jurnal .=" AND (gl_rtrans.tgl >='$_GET[mulai]' AND gl_rtrans.tgl <= '$_GET[sampai]')";
}
if($_GET[barang]<>'ALL' AND $_GET[barang]<>'') {
	$query_data .=" AND gl_trans.barang='$_GET[barang]'";
	$query_jurnal .=" AND gl_trans.barang='$_GET[barang]'";
}
$query_data .=" group by gl_trans.id desc";
$query_jurnal .=" group by gl_trans.id desc";
$data = mysql_query($query_data, $con_gl) or die(mysql_error());
$row_data = mysql_fetch_assoc($data);
$totalRows_data = mysql_num_rows($data);

$queryString_data = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_data") == false && 
        stristr($param, "totalRows_data") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_data = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_data = sprintf("&totalRows_data=%d%s", $totalRows_data, $queryString_data);
include('include/widget_export_stok.php');
?>
<link href="css/ui.datepicker.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="js/ui.datepicker.js"></script>
<script type="text/javascript">
<!--
$(function()
      {
        $('.calendar').datepicker({
            appendText : "",
            dateFormat : 'yy/mm/dd'
          });
      });
</script>
<script type="text/javascript">
$(document).ready(function(){
$(".flipsearch").click(function(){
	$("form.tanggalsearch").hide() 
    $(".objsearch").slideToggle("fast");
  });
});

$(document).ready(function(){
$(".fliptanggal").click(function(){
	$("form.objsearch").hide();
    $(".tanggalsearch").slideToggle("fast");
  });
});
</script>
<h1>Kartu Stok</h1>
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="datatable"> 
  <tr align="right" class="hide">
    <td colspan="9">
	<?php
	mysql_select_db($database_gl_jasa, $con_gl);
$query_cr_gud = "SELECT gl_barang.id, gl_barang.barang, gl_kelompok.kelompok, gl_kelompok.jenis FROM gl_barang, gl_kelompok WHERE gl_kelompok.id=gl_barang.kelompok AND gl_kelompok.jenis='2' ORDER BY barang ASC";
$cr_gud = mysql_query($query_cr_gud, $con_gl) or die(mysql_error());
$row_cr_gud = mysql_fetch_assoc($cr_gud);
$totalRows_cr_gud = mysql_num_rows($cr_gud);
?>
<form id="search" name="search" method="post" action="">
	    <label>Tanggal :
          <input name="mulai" type="text" id="mulai" value="<?php echo $_GET[mulai];?>" size="15" maxlength="15" class="calendar"/></label> s/d <label><input name="sampai" type="text" id="sampai" value="<?php echo $_GET[sampai];?>" size="15" maxlength="15" class="calendar"/>
        </label>
        <label><select name="cr_barang">
	      <option value="ALL">Barang</option>
	      <?php
do {  
?>
	      <option value="<?php echo $row_cr_gud['id']?>"><?php echo $row_cr_gud['barang']?></option>
	      <?php
} while ($row_cr_gud = mysql_fetch_assoc($cr_gud));
  $rows = mysql_num_rows($cr_gud);
  if($rows > 0) {
      mysql_data_seek($cr_gud, 0);
	  $row_cr_gud = mysql_fetch_assoc($cr_gud);
  }
?>
        </select></label><label>
      <input name="cr_txt" type="text" id="cr_txt" value="<?php echo $_REQUEST['cr_txt'];?>" size="30" maxlength="100"/>
        </label>
        <label>
      <input name="Search" type="submit" id="Search" value="Search" />
        </label>
	  </form>
	<?php
	if(isset($_POST['Search'])) {
		echo "<script>window.location=\"index.php?component=kartustok&mulai=".$_POST[mulai]."&sampai=".$_POST[sampai]."&cr_txt=".str_replace(" ","+",$_POST[cr_txt])."&barang=".$_POST[cr_barang]."\";</script>";
	}
	?>	</td>
  </tr>
  <tr>
    <th width="8%"><strong>Tanggal</strong></th>
    <th width="10%"><strong>No Referensi</strong></th>
    <th><strong>Barang</strong></th>
    <th width="10%" align="center"><strong>Masuk</strong></th>
    <th width="10%" align="center"><strong>Harga</strong></th>
    <th width="10%" align="center"><strong>Total</strong></th>
    <th width="10%" align="center"><strong>Keluar</strong></th>
    <th width="10%" align="center"><strong>Harga</strong></th>
    <th width="10%" align="center"><strong>Total</strong></th>
  </tr>
  <?php if($totalRows_data > 0) { ?>
  <?php do { ?>
  <?php
mysql_select_db($database_gl_jasa, $con_gl);
$query_hitung = "SELECT sum(gl_trans.total) FROM gl_trans WHERE gl_trans.no_ref='$row_data[no_ref]' AND pos='K'";
$hitung = mysql_query($query_hitung, $con_gl) or die(mysql_error());
$row_hitung = mysql_fetch_assoc($hitung);
$totalRows_hitung = mysql_num_rows($hitung);
  ?>
    <tr valign="top" bgcolor="#FFFFFF">
      <td align="center"><?php tanggal($row_data['tgl'],"tampilkan");?></td>
      <td width="10%" align="center"><a href="include/jurnalvoucher_transaksi.php?referensi=<?php echo $row_data['no_ref'];?>" onclick="NewWindow(this.href,'name','1050','300','yes');return false" title="Jurnal Voucher"><?php echo $row_data['no_ref']; ?></a></td>
      <td><?php echo $row_data['barang']; ?></td>
      <td align="center"><?php if($row_data['jenis']==6) { ?><?php echo $row_data['jumlah']; $jummasuk += $row_data['jumlah'];?> <?php echo $row_data['satuan']; ?><?php }else{ ?> - <?php } ?></td>
      <td align="right"><?php if($row_data['jenis']==6) { ?><?php echo number_format($row_data['hargasatuan'],0,',','.').",-"; ?><?php }else{ ?> - <?php } ?></td>
      <td align="right"><?php if($row_data['jenis']==6) { ?><?php echo number_format($row_data['total'],0,',','.').",-"; $totmasuk += $row_data['total'];?><?php }else{ ?> - <?php } ?></td>
      <td align="center"><?php if($row_data['jenis']==7) { ?>
          <?php echo $row_data['jumlah']; ?> <?php echo $row_data['satuan']; ?>
        <?php }else{ ?>
        -
  <?php } ?></td>
      <td align="right"><?php if($row_data['jenis']==7) { ?>
          <?php echo number_format($row_data['hargasatuan'],0,',','.').",-"; ?>
        <?php }else{ ?>
        -
  <?php } ?></td>
      <td align="right"><?php if($row_data['jenis']==7) { ?>
          <?php echo number_format($row_data['total'],0,',','.').",-"; $totkeluar +=$row_data['total'];?>
        <?php }else{ ?>
        -
  <?php } ?></td>
    </tr>
    <?php } while ($row_data = mysql_fetch_assoc($data)); ?>
	<tr valign="top" bgcolor="#FFFFFF">
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="center">&nbsp;</td>
      <td align="right">Total : </td>
      <td align="right"><?php echo number_format($totmasuk,0,',','.').",-";?></td>
      <td align="right" bgcolor="#FFFFFF">&nbsp;</td>
      <td align="right" bgcolor="#FFFFFF">Total : </td>
      <td align="right" bgcolor="#FFFFFF"><?php echo number_format($totkeluar,0,',','.').",-";?></td>
    </tr>
	<?php if($totalRows_data > $maxRows_data) { ?>
    <tr class="hide">
      <td colspan="9"><table border="0" align="left">
          <tr>
            <td width="23%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, 0, $queryString_data); ?>"><img src="First.gif" border=0></a>
                <?php } // Show if not first page ?>            </td>
            <td width="31%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, max(0, $pageNum_data - 1), $queryString_data); ?>"><img src="Previous.gif" border=0></a>
                <?php } // Show if not first page ?>            </td>
            <td width="23%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, min($totalPages_data, $pageNum_data + 1), $queryString_data); ?>"><img src="Next.gif" border=0></a>
                <?php } // Show if not last page ?>            </td>
            <td width="23%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, $totalPages_data, $queryString_data); ?>"><img src="Last.gif" border=0></a>
                <?php } // Show if not last page ?>            </td>
          </tr>
        </table></td>
    </tr>
	<?php } ?>
   <?php }else{ ?>
  <tr bgcolor="#FFFFFF">
    <td colspan="9">Data tidak ada !!! </td>
  </tr>
  <?php } ?>
</table>
<?php
mysql_free_result($cr_gud);
?>
